package cn.fjc920.siaa.fragments

import android.app.Dialog
import android.app.AlertDialog
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import cn.fjc920.siaa.activitys.HomePageActivity
import cn.fjc920.siaa.activitys.AddActivity
import cn.fjc920.siaa.internetUtils.LoadingDialog
import cn.fjc920.siaa.internetUtils.OkhttpUtils
import cn.fjc920.siaa.internetUtils.OkhttpUtils.DataCallBack
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.DEBUG_PROPERTY_VALUE_AUTO
import okhttp3.Request
import okhttp3.internal.platform.android.AndroidLog
import java.io.IOException

class MyDialogFragment(
    private val editUrl: String,
    private val deleteUrl: String,
    private val _csrf: String,
    private val hoursId: String,
    private val hours:String
) : DialogFragment() {

    private lateinit var map: HashMap<String, String>
    private lateinit var context: HomePageActivity
    private lateinit var items: ArrayList<String>

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        context = activity as HomePageActivity

        items = ArrayList()
        items.add("编辑")
        items.add("删除")
        items.add("复制")

        val builder = AlertDialog.Builder(context).apply {
            setTitle("操作")
            setItems(items.toTypedArray(), DialogInterface.OnClickListener { dialog, which ->
                when(which) {
                    0 -> {
                        AddActivity.actionStart(context, "编辑工时", editUrl, hoursId)
                        dialog.dismiss()
                    }
                    1 -> {
                        deleteOrNot()
                        dialog.dismiss()
                    }
                    2 -> {
                        val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                        val cd = ClipData.newPlainText("hoursData",hours)
                        cm.setPrimaryClip(cd)
                        Toast.makeText(context,"复制成功！",Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
        return builder.create()
    }

    private fun deleteOrNot() {
       AlertDialog.Builder(context).apply {
            setTitle("温馨提示！")
            setMessage("确定要删除吗？")
            setNegativeButton("取消") { d, _ ->
                d.dismiss()
            }
            setPositiveButton("确定") { d, _ ->
                deleteHours()
                d.dismiss()
            }
           create()
           show()
        }
    }

    private fun deleteHours() {
        map = HashMap()
        map["id"] = hoursId
        map["_csrf"] = _csrf
        OkhttpUtils.postAsync(deleteUrl, map, object : DataCallBack {

            val loadingDialog = LoadingDialog(context).apply {
                show("删除中...")
            }

            override fun requestFailure(request: Request?, e: IOException?) {
                loadingDialog.dismiss()
                Toast.makeText(context, "操作失败，请检查网络！", Toast.LENGTH_SHORT).show()
            }
            override fun requestSuccess(result: String) {
                loadingDialog.dismiss()
                Toast.makeText(context, "已删除", Toast.LENGTH_SHORT).show()
                HomeFragment.homeTodayFragment.refresh()
                HomeFragment.homeAllFragment.refresh()
            }
        })
    }
}